﻿using Max.PH5_2011A.Domain;
using Max.PH5_2011A.DTO;
using Max.PH5_2011A.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2011A.API.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("member")]
    [ApiController]
    public class MemberInfoController : ControllerBase
    {
        IMemberInfoService _service;
        public MemberInfoController(IMemberInfoService service)
        {
            _service = service;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(MemberInfoModel model)
        {
            model.Password = MD5Hash.Hash.Content(model.Password);
            _service.Insert(model);
            return Ok();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public IActionResult Login(MemberLoginInputDto dto)
        {
            ResponseResultDto<dynamic> response = new ResponseResultDto<dynamic>();

            if (!ModelState.IsValid)
            {
                response.Code = ResponseStateCode.PARAMNOVALID;
                response.Message = "参数无效";
                return Ok(response);
            }

            var result =  _service.Login(dto.LoginName, dto.Password);
            response.Code = result.Item1;
            response.Message = result.Item2;
            response.Data = result.Item3;
            return Ok(response);
        }
    }
}
